package logic

import (
	"context"
	"net/http"
	"shuqi_novel/pkg"
	"strings"

	"shuqi_novel/api/internal/svc"
	"shuqi_novel/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpLodeLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpLodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpLodeLogic {
	return &UpLodeLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpLodeLogic) UpLode(req **http.Request) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	file, m, err := (*req).FormFile("image")
	if err != nil {
		return nil, err
	}
	if !(strings.Contains(m.Filename, ".jpg") || strings.Contains(m.Filename, ".png")) {
		return &types.Response{
			Code:    500,
			Message: "图片格式错误",
			Data:    nil,
		}, nil
	}
	if m.Size >= 2<<20 {
		return &types.Response{
			Code:    500,
			Message: "图片过大",
			Data:    nil,
		}, nil
	}
	lode := pkg.UpLode(file, m)
	return &types.Response{
		Code:    200,
		Message: "上传成功",
		Data:    lode,
	}, nil
}
